package cn.com.dhcc.turbo.service.monitor;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.NetUtil;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.app.pub.core.tag.AppTagEl;
import cn.com.dhcc.turbo.dao.exch.CarvedDiscsDataDao;
import cn.com.dhcc.turbo.entity.monitor.CrossNetMonitor;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.registry.BaseInfoService;
/**
 * 跨网监控
 * 
 * @日期 2014-12-4 上午11:41:55
 * @作者  高强
 */
@Service
public class CrossNetMonitorService extends BaseService{

	@SuppressWarnings("deprecation")
	Logger logger = Log.getExistLogger(LOGTYPE.MONITOR);
	@Autowired
	CarvedDiscsDataDao carvedDiscsDataDao;
	
	@Autowired
	BaseInfoService baseInfoService;
	@SuppressWarnings("rawtypes")
	public CrossNetMonitor getMonitor(){
		CrossNetMonitor monitor=new CrossNetMonitor();
	
		NodeInfo localNode=null;
		try {
			localNode = baseInfoService.getCurrentNodeInfo();
		} catch (ServiceException e1) {
			logger.error(e1);
		}
		if ("1".equals(localNode.getHasGap())) {
			//获取网闸的状态
			String ip=NodeInfoParam.GAP_PARAM.IP;
			int port=NodeInfoParam.GAP_PARAM.Port;
			String username=NodeInfoParam.GAP_PARAM.UserName;
			String password=NodeInfoParam.GAP_PARAM.Password;
			String dir=NodeInfoParam.GAP_PARAM.Home;
		   if (NetUtil.canConnect(ip, port)) {
			   monitor.setGapStatus("1");
			   //访问网闸，获取文件量，和文件的大小
			   Long size=0l;
			   int sum=0;
			   FTPClient client=new FTPClient();
			   client.setConnectTimeout(5000);
			   try {
				   client.connect(ip, port);
				   client.login(username, password);
				  if((dir!=null && !"".endsWith(dir) && client.changeWorkingDirectory(dir)) || (dir==null || "".equals(dir))){
					   FTPFile[] files=client.listFiles();
					   sum=files.length;//文件数量
					   for (FTPFile ftpFile : files) {
						   size+=ftpFile.getSize();
					   }
					   monitor.setGapToSendNum(sum+"");
					   monitor.setGapToSendSize(AppTagEl.formatByte(size.toString()));
				  }else{
				      monitor.setGapToSendNum("0");
				      monitor.setGapToSendSize("0B");
				  }
				} catch (IOException e) {
					monitor.setGapStatus("0");
			    	monitor.setGapToSendNum("0");
			    	monitor.setGapToSendSize("0B");
				}finally{
					try {
						client.disconnect();
					} catch (IOException e) {
						//
					}
				}
		    }else{
		    	monitor.setGapStatus("0");
		    	monitor.setGapToSendNum("0");
		    	monitor.setGapToSendSize("0B");
		    }
		}
				
		//获取待刻盘的梳理  0待刻盘 1 已刻盘
	    String sql="select count(*) as sum from "+SI_TABLE("TURBO_CARVED_DISCS_DATA")+ " where status like '%0%'";
	    try {
			Map map=CM.getDao().getMap("获取待刻盘的梳理", sql, new Object[]{});
			if(map!=null && map.get("sum")!=null){
				monitor.setCarvedDiscsSum(map.get("sum").toString());
			}else{
				monitor.setCarvedDiscsSum("0");
			}
		} catch (DaoException e) {
			logger.error(e);
		}
	   WhereCondition condtion=new WhereCondition();
	   condtion.where().like("status", "%0%");
	  try {
		Long filesize= carvedDiscsDataDao.sumFileSize(condtion);
		monitor.setCarvedDissSize(AppTagEl.formatByte(filesize.toString()));
	 } catch (DaoException e) {
		 logger.error(e);
	 }
	   
		return monitor;
	}
	
	
	
}
